package com.hudongwx.my_shian.fragment;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hudongwx.my_shian.R;
import com.hudongwx.my_shian.fragment.communityFragment.Health_3_Fragment;
import com.hudongwx.my_shian.fragment.communityFragment.Life_5_Fragment;
import com.hudongwx.my_shian.fragment.communityFragment.Motion_4_Fragment;
import com.hudongwx.my_shian.fragment.communityFragment.My_Sun_2_Fragment;
import com.hudongwx.my_shian.fragment.communityFragment.Recommend_1_Fragment;

/**
 * A simple {@link Fragment} subclass.
 */
public class CommunityFragment extends Fragment {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    View view=null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view==null){
            view = inflater.inflate(R.layout.fragment_community, container, false);
            //找出控件
            tabLayout = (TabLayout) view.findViewById(R.id.community_tabLayout);
            viewPager = (ViewPager) view.findViewById(R.id.community_vp);

            //适配器
            MyPagerAdapter myPagerAdapter = new MyPagerAdapter(getActivity().getSupportFragmentManager());
            //绑定适配器
            viewPager.setAdapter(myPagerAdapter);

            //设置选项卡的显示方式//所有选项卡平分父视图
            tabLayout.setTabMode(TabLayout.MODE_FIXED);
            tabLayout.setupWithViewPager(viewPager);

        }

        return view;
    }

    class MyPagerAdapter extends FragmentPagerAdapter {
        Fragment[] fragments = new Fragment[5];

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
//                    if (fragments[position] == null)
                        fragments[position] = new Recommend_1_Fragment();
                    break;
                case 1:
//                    if (fragments[position] == null)
                        fragments[position] = new My_Sun_2_Fragment();
                    break;
                case 2:
//                    if (fragments[position] == null)
                        fragments[position] = new Health_3_Fragment();
                    break;
                case 3:
//                    if (fragments[position] == null)
                        fragments[position] = new Motion_4_Fragment();
                    break;
                case 4:
//                    if (fragments[position] == null)
                        fragments[position] = new Life_5_Fragment();
                    break;
            }
            return fragments[position];
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            String title = null;
            switch (position) {
                case 0:
                    title = "推荐";
                    break;
                case 1:
                    title = "我来晒单";
                    break;
                case 2:
                    title = "饮食养生";
                    break;
                case 3:
                    title = "运动健康";
                    break;
                case 4:
                    title = "品质生活";
                    break;
            }
            return title;
        }
    }

}
